package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import dao.StudentMapper;
import domain.ContractTypeExample;
import domain.Student;
import domain.StudentExample;

/**
 * User: xinxin
 * Date: 2016-11-05
 * Time: 20:56
 * FIXME
 */
@Service
public class StudentServiceImp implements StudentServiceI {
    @Autowired
    StudentMapper mStudentMapper;

    @Override
    public boolean register(Student student) {
        return mStudentMapper.insert(student) > 0;
    }

    /**
     * code已经存在返回false，不存在返回true
     * @param studentCode
     * @return
     */
    public boolean checkCode(String studentCode) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andCodeEqualTo(studentCode);
        final List<Student> students = mStudentMapper.selectByExample(studentExample);
        if (students != null) {
            return false;
        }
        return true;
    }

    @Override
    public Student login(String studentCode, String password) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andCodeEqualTo(studentCode);
        criteria.andPasswordEqualTo(password);
//        studentExample.or(criteria);
        final List<Student> students = mStudentMapper.selectByExample(studentExample);
        if (students != null) {
            return students.get(0);
        }
        return null;
    }
}
